from fastapi import APIRouter, Depends,File,UploadFile
import asyncio

from app.schema.base import ListArgsSchema, RespBaseSchema, RespIdSchema, RespListSchema, ListKeySchema
from app.utils.custom_route import CustomRoute
from app.schema.auth import TokenData
from app.utils.upload import MpUploadOssHelper
from ..schema.file import FileInfoSchema,OSSResponseSchema,OSSParmasSchema
from ..service.file import FileService
from app.utils.auth import get_current_user
from app.schema.file import *
router = APIRouter(route_class=CustomRoute)

@router.get('/getOssSignature',response_model=OSSResponseSchema)
async def upload():
    data=MpUploadOssHelper().create_upload_params()
    print('data',data)
    # print('data', OSSResponseSchema(code=200,data=data))
    return OSSResponseSchema(code=200,data=data)

@router.post('/list')
async def list(*, args: ListArgsSchema, auth_data: TokenData = Depends(get_current_user)):
    try:
        args.user_id = auth_data.id
        args.keys = [
            ListKeySchema(key='id'),
            ListKeySchema(key='size'),
            ListKeySchema(key='extension'),
            ListKeySchema(key='name'),
            ListKeySchema(key='created_time'),
            ListKeySchema(key='status')
        ]

        resp = RespDataSchema()
        resp.data = {'list': FileService(auth_data).list(args).list}
        return resp
    except BaseException as e:
        return RespListSchema(code=50001, message='获取失败')


# @router.get('/{id}', response_model=FileRespDetailSchema)
# async def read(id: int, auth_data: dict = Depends(get_auth_data)):
#     resp = FileRespDetailSchema()
#     resp.detail = FileService(auth_data).read(id)
#     return resp


@router.post('', response_model=RespIdSchema, response_model_exclude_none=True)
async def create(*, info: FileInfoSchema, auth_data: dict = Depends(get_current_user)):
    return FileService(auth_data).create(info)


@router.put('/{id}', response_model=RespBaseSchema)
async def update(*, info: FileInfoSchema, auth_data: dict = Depends(get_current_user)):
    return FileService(auth_data).update(info)


@router.delete('/{id}', response_model=RespBaseSchema)
async def delete(id: int, auth_data: dict = Depends(get_current_user)):
    return FileService(auth_data).delete(id)

@router.post('/create')
async def create_file(file: UploadFileSchema, auth_data: dict = Depends(get_current_user)):
    try:
        file.user_id=auth_data.id
        return FileService(auth_data).create_file(file, auth_data)
    except BaseException as e:
        FileService(auth_data).dao.db.sess.rollback()
        return  RespBaseSchema(code=50001,message='上传失败')

@router.post('/loadmilvus', response_model=RespIdSchema)
async def upload_file(file_milvus: MilvusFileSchema, auth_data: dict = Depends(get_current_user)):
    try:
        fileService=FileService(auth_data)
        asyncio.create_task(fileService.load_milvus(file_milvus, auth_data))
        return RespBaseSchema()
    except BaseException as e:
        fileService.dao.db.sess.rollback()
        return RespBaseSchema(code=50001,message='加载失败')
    
@router.post('/delete', response_model=RespIdSchema)
async def delete_file(file: DeleteFileSchema, auth_data: dict = Depends(get_current_user)):
    try:
        fileService=FileService(auth_data)
        await fileService.delete_file(file.id, auth_data)
        return RespBaseSchema()
    except BaseException as e: 
        fileService.dao.db.sess.rollback()
        return RespBaseSchema(code=50001,message='删除失败')
